<?php 
class Home extends BZ_Controller
{
    public function _init()
    {
        $this->load->helper('city');
        $this->load->model('advert_model', 'advert');
        $this->load->model('scenery_base_model','scenery_base');
        $this->load->model('region_model', 'region');
        $this->load->model('cms_block_model', 'cms_block');
    }
    
    public function index()
    {
        $parentId = 31;  //省份编号
        $regionId = 383; //城市编号
        $regionName = get_cookie('city') ? get_cookie('city') : '杭州';
        $result = $this->region->getChinaRegion(array('region_type'=>2));
        foreach ($result->result() as $item) {
            if ($regionName == $item->region_name) {
                $parentId   = $item->parent_id;
                $regionId   = $item->region_id;
                $regionName = $item->region_name;
                break;
            }
        }
        $data = array(
            'advert'       => $this->advert->findByParams(['status'=> 1, 'opentype' => 'link', 'position' => 14], '*', null, 'sort ASC')->result(), //广告图片
            'allCities'    => $result->result(),
            'sceneryOnSal' => $this->scenery_base->sceneryOnSal(array('cityId'=>$regionId, 'is_scan'=>1), 6)->result(), //特价专区
            'sceneryHot'   => $this->scenery_base->sceneryRecommend(array('cityId'=>$regionId, 'is_scan'=>1), 12, 0)->result(),//热门景区
            'cmsBlocks'    => $this->cms_block->findByBlockIds(array('wap_scenery_popular_search')),
        );
        $data['parentId'] = $parentId; //省份ID
        $data['cityId']   = $regionId; //城市ID
        $data['cityName'] = $regionName; //城市名称
        $data['currentUrl'] = current_url();
        $this->load->view('scenic/home/newindex', $data);
    }
    
    /**
     * ajax获取当前地址，并存入cookie
     */
    public function ajaxCurrentCity()
    {
        $location  = $this->input->get('location');
        $coordtype = $this->input->get('coordtype');
        $output    = $this->input->get('output');
        $pois      = $this->input->get('pois');
        
        $cityName = locationCurrentCity($location, $coordtype, $output, $pois);
        if ($cityName) {
            set_cookie('city', $cityName['city'], 43250);
            set_cookie('province', $cityName['province'], 43250);
        }
        echo json_encode(array(
            'status'   => true,
            'messages' => $cityName['city'],
        ));exit;
    }
    
    /**
     * 切换当前城市
     */
    public function changeCity()
    {
        $cityName = $this->input->get('cityName');
        if ($cityName) {
            set_cookie('city', $cityName, 43250);
        }
        redirect('scenic/home/index');
    }

}